package cn.stylefeng.guns.modular.business.frequency.orderfinish.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.FinishRecordDTO;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.OrderFinishRecordVO;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.param.ErrorMsg;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.param.OrderDTO;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderFinishRecord;
import cn.stylefeng.guns.modular.business.frequency.statistics.PkgOrderFinish;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import cn.stylefeng.guns.sys.modular.user.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.service.OrderFinishService;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.OrderFinishParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单完成确认表 控制器
 *
 * @author: yujie
 * @date: 2023/05/09 17:16
 */
@RestController
public class OrderFinishController {

    @Resource
    private OrderFinishService finishService;

    @Resource
    private SysUserMapper userMapper;


    @Resource
    private BusinessUtil businessUtil;



    @ApiOperation(value = "待交付订单分页列表")
    @GetMapping("/wait")
    public ResponseData getWaitFinishListPage( OrderDTO orderDTO) {
        getFinishWaitQueryParam(orderDTO);
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("orderId");
        }
        Page list = finishService.getWaitFinishListPage(PageFactory.defaultPage(), orderDTO);
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(list, orderDTO));
    }

    //@Log("订单批量交付交付")
    @ApiOperation(value = "交付")
    @PostMapping
    public ResponseData finish(@RequestBody OrderFinishRecordVO orderFinishRecordVO) {
        ErrorMsg errorMsg = new ErrorMsg();
        if (!finishService.checkFinishAdd(orderFinishRecordVO, errorMsg)) {
            return ResponseData.andaError(errorMsg.getErrorMsg());
        }
        //String userId = SecurityUtil.getCurrentUserId(1);
        Long userId = LoginContextHolder.me().getSysLoginUserId();
        finishService.finish(userId, orderFinishRecordVO);
        return ResponseData.andaSuccess();
    }

    //@Log("直交客户确认到达")
    @ApiOperation("直交客户确认到达")
    @PostMapping("/level")
    public ResponseData levelFinish(@RequestBody OrderFinishRecordVO orderFinishRecordVO) {
        StringBuilder errorMsg = new StringBuilder();
        if (!finishService.checkLevelFinish(orderFinishRecordVO, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        finishService.levelFinish(orderFinishRecordVO);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "已完成交付分页列表")
    @GetMapping("/record")
    public ResponseData getOrderFinishListPage(Page page, FinishRecordDTO orderDTO) {
        // 排序
        if (StringUtils.isBlank(orderDTO.getColumn())) {
            orderDTO.setColumn("id");
        }
        getFinishOverQueryParam(orderDTO);
        Page<PkgOrderFinish> list = finishService.getFinishOverListPage(page, orderDTO);
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(list, orderDTO));
    }

    @ApiOperation(value = "已完成交付详情")
    @GetMapping("/over/{id}")
    public ResponseData getFinishOverDetail(@PathVariable String id) {
        PkgOrderFinishRecord orderFinishRecord = finishService.getPkgOrderFinishRecordDetail(id);
        return ResponseData.andaSuccess(orderFinishRecord);
    }

    @ApiOperation(value = "订单转账凭证")
    @GetMapping("/{orderId}/transfer-voucher")
    public ResponseData getOrderFinishPayImage(@PathVariable Long orderId) {
        List<String> list = finishService.getOrderFinishPayImage(orderId);
        return ResponseData.andaSuccess(list);
    }

    private void getFinishOverQueryParam(FinishRecordDTO orderDTO) {
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);
    }

    private void getFinishWaitQueryParam(OrderDTO orderDTO) {
        orderDTO.setSiteId(businessUtil.getSiteId());
        orderDTO.setUserId(LoginContextHolder.me().getSysLoginUserId().toString());
        List<Long> empByOrgList = businessUtil.getEmpByOrgList();
        orderDTO.setDataList(empByOrgList);
    }



}
